package com.dldata.devtools30.controller.sysform;

import com.dldata.devtools30.entity.DevtFormTypeEntity;
import com.dldata.devtools30.service.sysform.DevtFormTypeService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;
import java.util.stream.Collectors;

/**

 */
@Controller
@RequestMapping(value = "/V_DevtFormTypeController")
public class V_DevtFormTypeController {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private DevtFormTypeService customFormTypeService;

    @RequestMapping("toPage")
    public String toSysFormTypeList(){

        return "thymeleaf/devtools30/sysform/v_SysFormType";
    }


    @RequestMapping("toEdit")
    public String toSysFormTypeEdit(String id,Model model){
        List<DevtFormTypeEntity>  sysCrfFormTypeEntities= customFormTypeService.findByTypeId(id);
        if(sysCrfFormTypeEntities.size()>0){
            model.addAttribute("data",sysCrfFormTypeEntities.get(0));
        }else{
            DevtFormTypeEntity sysCrfFormTypeEntity= new DevtFormTypeEntity();
            sysCrfFormTypeEntity.setfTypeId("0");
            List<DevtFormTypeEntity> allFormField = customFormTypeService.getAllFormType();
            int xh = allFormField.size() + 1;
            for (int i = 0; i < allFormField.size(); i++) {
                DevtFormTypeEntity fieldEntity = allFormField.get(i);
                xh=fieldEntity.getXh()>=xh?(fieldEntity.getXh()+1):xh;
            }
            sysCrfFormTypeEntity.setXh(xh);

            model.addAttribute("data",sysCrfFormTypeEntity);
        }
        List<DevtFormTypeEntity>  sysCrfFormTypeEntitiesTop = customFormTypeService.getAllSubFormType("0");
        sysCrfFormTypeEntitiesTop=sysCrfFormTypeEntitiesTop.stream().filter(sysCrfFormTypeEntity -> !sysCrfFormTypeEntity.getId().equals(id)).collect(Collectors.toList());
        model.addAttribute("ftypeList",sysCrfFormTypeEntitiesTop);
        return "thymeleaf/devtools30/sysform/v_SysFormTypeEdit";
    }
}
